﻿
#region "File Header"

//==============================================================================
//  FileName    :	ConfigurationUtility.cs
//
//  Description :	The ConfigurationFucntion class is a lot of functions to get or set a value form a config file.
//				
//
//  Copyright (C) 2008, MasterCai Inc.
//  All rights reserved.
//														 ... CaiShengrong
//==============================================================================
//
//----------------------------------------------------------------------------
//  Update Log:
//
//  Status          Date            Name                    BUG-ID
//  ------------------------------------------------------------------------
//  Created         2007/03/13     	CaiShengrong	 		None
//	Modified		
//	Modified		
//	Modified		
//	Modified		
//	Modified		
//	Modified		
//----------------------------------------------------------------------------

#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Xml;
using System.IO;

namespace SPMS.Utility
{
    /// <summary>
    /// 
    /// </summary>
    /// <remarks></remarks>
    /// <history>
    /// [CaiShengRong]   2009/3/13 15:44    Created  您的潜力，俺的动力
    /// </history>
    public static class ConfigurationUtility
    {
        #region "Get Value"

        /// <summary>
        /// get application config file' appsetting one point's value
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public static string GetValue(string name)
        {
            if((name == null) || (name == ""))
            {
                return null;
            }

            return ConfigurationManager.AppSettings.Get(name);
        }

        /// <summary>
        /// GetConnString
        /// </summary>
        /// <param name="connectionName">Name of the connection.</param>
        /// <returns></returns>
        public static string GetConnString(string connectionName)
        {
            if((connectionName == null) || (connectionName == ""))
            {
                return null;
            }
            return ConfigurationManager.ConnectionStrings[connectionName].ConnectionString;
        }

        /// <summary>
        /// get application config file' appsetting one point's value
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="configFileName">the config file name with the full path</param>
        /// <returns></returns>
        public static string GetValue(string name, string configFileName)
        {
            if((name == null) || (name == ""))
            {
                return null;
            }

            XmlNodeList nodes = null;
            XmlDocument doc = new XmlDocument();

            //find in a file
            doc.Load(configFileName);
            nodes = doc.GetElementsByTagName("add");

            foreach(XmlNode node in nodes)
            {
                //circle to find
                if(node.Attributes["key"].Value == name)
                {
                    //if find then return
                    return node.Attributes["value"].Value;
                }
            }

            return null;

        }

        #endregion

        #region "Set Value"

        /// <summary>
        /// set the config file's value
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        public static void SetValue(string name, string value)
        {
            if((name == null) || (name == ""))
            {
                //if no the node
                return;
            }

            ConfigurationManager.AppSettings.Set(name, value);

            SetValue(name, value, Path.GetFullPath(System.Windows.Forms.Application.ExecutablePath + ".config"));



        }


        /// <summary>
        /// set the config file's value
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <param name="configFileName">the config file name with the full path</param>
        public static void SetValue(string name, string value, string configFileName)
        {
            if((name == null) || (name == ""))
            {
                return;
            }

            XmlNode node = null;
            XmlDocument doc = new XmlDocument();

            //load the xml file
            doc.Load(configFileName);

            //Get all emements 
            node = doc.SelectSingleNode("configuration");
            foreach(XmlNode appNode in node)
            {

                if(appNode.Name == "appSettings")
                {
                    foreach(XmlNode xNode in appNode)
                    {
                        //circle to find
                        if((xNode.Name == "add") && (xNode.Attributes["key"].Value == name))
                        {
                            //if find ,set the value
                            xNode.Attributes["value"].Value = value;
                            //exit for
                            break;
                        }
                    }
                }


            }


            doc.Save(configFileName);

        }

        #endregion
    }
}
